package sy.service.base.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import sy.dao.base.UserDaoI;
import sy.model.base.Tuser;
import sy.service.base.UserServiceI;
import sy.util.base.QueryFilter;

@Service("userService")
public class UserServiceImpl extends BaseServiceImpl<Tuser, String> implements UserServiceI {

	@Resource(name = "userDao")
	private UserDaoI userDao;

	/**
	 * 重写get方法，使获得user的时候，同时可以获得user对象里面的对象的信息
	 * 
	 * @param pk
	 * @return
	 */
	@Override
	public Tuser get(String pk) {
		QueryFilter filter = new QueryFilter();
		filter.addFilter("Q_t.id_EQ_S", pk);
		List<Tuser> ul = this.findAll(filter);
		if (ul != null && ul.size() > 0) {
			return ul.get(0);
		}
		return null;
	}

	/**
	 * 重写findAll方法，查询的时候，可以添加user里面的对象属性的条件
	 */
	@Override
	public List<Tuser> findAll(QueryFilter filter) {
		String hql = " select count(distinct t) from Tuser t ";
		hql += " left join t.tuserTorganizations userorganizations ";
		hql += " join userorganizations.torganization organization ";
		hql += " left join t.tuserTroles userroles ";
		hql += " join userroles.trole role ";
		hql += " where 1=1 ";
		filter.setTotal(this.userDao.total(hql + filter.getWhere(), filter.getParams()));
		hql = " select distinct t from Tuser t ";
		hql += " left join fetch t.tuserTorganizations userorganizations ";
		hql += " join fetch userorganizations.torganization organization ";
		hql += " left join fetch t.tuserTroles userroles ";
		hql += " join fetch userroles.trole role ";
		hql += " where 1=1 ";
		return this.userDao.findAll(hql + filter.getWhere() + filter.getSortAndOrder(), filter.getParams(), filter.getPage(), filter.getRows());
	}

}
